/**
 * 
 */
package com.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.database.TransactionManager;
import com.model.CotacaoBean;

/**
 * @author Isac
 *
 */
public class CotacaoDao {

	private String TABLE_COTACAO = "cotacao";
	private String COLN_ID     = "cot_id";
	private String COLN_EMP_ID = "emp_id";
	private String COLN_PROD   = "prod_id";
	private String COLN_DATA   = "cot_data";
	private String COLN_HORA   = "cot_hora";
	private String COLN_VALOR  = "cot_valor";
	
	public static String createTableCotacao(){
		StringBuilder sb = new StringBuilder();
		sb.append(" create table cotacao(                                     \n");
		sb.append("    cot_id integer primary key autoincrement,              \n");
		sb.append("    emp_id integer,                                        \n");
		sb.append("    prod_id integer,                                       \n");
		sb.append("    cot_data text,                                         \n");
		sb.append("    cot_hora text,                                         \n");
		sb.append("    cot_valor decimal,                                     \n");
		sb.append("    foreign key (prod_id) references produto(prod_id),     \n");
		sb.append("    foreign key (emp_id) references empresa(emp_id));      \n");
		return sb.toString();
	}
	
	public void insert(TransactionManager transaction, CotacaoBean cotBean) {
		ContentValues initiaValues = new ContentValues();
		initiaValues.put(COLN_EMP_ID, cotBean.getEmp().getId());
		initiaValues.put(COLN_PROD, cotBean.getProd().getId());
		initiaValues.put(COLN_DATA, cotBean.getData());
		initiaValues.put(COLN_HORA, cotBean.getHora());
		initiaValues.put(COLN_VALOR, cotBean.getValor());
		transaction.prepare().insert(TABLE_COTACAO, null, initiaValues);
	}
	
	public boolean delete(TransactionManager transaction,CotacaoBean bean){
		return transaction.prepare().delete(TABLE_COTACAO,COLN_ID +" = "+bean.getId(), null) > 0 ;
		
	}
	
	public long update(TransactionManager transaction, CotacaoBean cotBean ) {
		ContentValues values = new ContentValues();
		values.put(COLN_DATA, cotBean.getData());
		values.put(COLN_HORA, cotBean.getHora());
		values.put(COLN_VALOR, cotBean.getValor());
		return transaction.prepare().update(TABLE_COTACAO, values,COLN_ID +" = "+cotBean.getId(), null);
		
	}
	
	public CotacaoBean openById(TransactionManager transaction, long cotId) {
		StringBuilder sql = new StringBuilder();
		sql.append("        select * from                  ");
		sql.append(          TABLE_COTACAO                  );
		sql.append("         where "+COLN_ID+" = "+ cotId   );
		CotacaoBean cotBean = new CotacaoBean();
		Cursor cursor = transaction.prepare().rawQuery(sql.toString(), null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst(); 
			cotBean.setId(cursor.getInt(cursor.getColumnIndex(COLN_ID)));
			cotBean.getEmp().setId(cursor.getColumnIndex(COLN_EMP_ID));
			cotBean.getProd().setId(cursor.getColumnIndex(COLN_PROD));
			cotBean.setData(cursor.getString(cursor.getColumnIndex(COLN_DATA)));
			cotBean.setHora(cursor.getString(cursor.getColumnIndex(COLN_HORA)));
			cotBean.setValor(cursor.getDouble(cursor.getColumnIndex(COLN_VALOR)));
			cursor.close();
			return cotBean;
		} else {
			cursor.close();
			return null;
		}		
	}
	
	public List<CotacaoBean> getAll(TransactionManager transaction) {
		StringBuilder sql = new StringBuilder();
		sql.append("        select * from                  ");
		sql.append(          TABLE_COTACAO                  );
		List<CotacaoBean> cotList = new ArrayList<CotacaoBean>();
		Cursor cursor = transaction.prepare().rawQuery(sql.toString(), null);
		while (cursor.moveToNext()) {
			CotacaoBean cotBean = new CotacaoBean();
			cotBean.setId(cursor.getInt(cursor.getColumnIndex(COLN_ID)));
			cotBean.getEmp().setId(cursor.getColumnIndex(COLN_EMP_ID));
			cotBean.getProd().setId(cursor.getColumnIndex(COLN_PROD));
			cotBean.setData(cursor.getString(cursor.getColumnIndex(COLN_DATA)));
			cotBean.setHora(cursor.getString(cursor.getColumnIndex(COLN_HORA)));
			cotBean.setValor(cursor.getDouble(cursor.getColumnIndex(COLN_VALOR)));
			cotList.add(cotBean);
		} 
			cursor.close();
			return cotList;
	}
	
}

